package com.rxjava;

import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

/**
 * @auther: cc.ly
 * @date: 2019/5/28/028
 */

public class RxJavaDemo01 {
    public static void testRun() {
        Observer<String> myObserver = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable disposable) {
            }

            @Override
            public void onNext(String s) {
                System.out.println(s);
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onComplete() {
            }
        };
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("s");
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.newThread()).subscribe(myObserver);
    }

    public static void testFlowTest() {
        Flowable.create(new FlowableOnSubscribe<String>() {
            @Override
            public void subscribe(FlowableEmitter<String> emitter) throws Exception {
                System.out.println(emitter.getClass());
                emitter.onNext("hello world");
                emitter.onNext("hello world2");
                System.out.println("subscribe run..");
            }
        }, BackpressureStrategy.MISSING).subscribeOn(Schedulers.newThread()).subscribe(new Subscriber<String>() {
            @Override
            public void onSubscribe(Subscription subscription) {
                //subscription.request(1);
            }

            @Override
            public void onNext(String s) {
                System.out.println(s);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    public static void main(String[] args) {
        // testRun();
        testFlowTest();
    }


}
